<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use DB;
use Hash;
use Mail;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class SelfController extends Controller
{
	 //进行遍历类别
    static public function getCate($pid){
        //定义一个空数组
        $cate=array();
        //通过传过来的pid查询类别表
        $data=DB::table('cate')->where('pid','=',$pid)->get();
        //遍历查询到的数据
        foreach($data as $k=>$v){
            //通过自身的id去掉用自身的方法(通过pid查询) 判断下面是否还存在子分类 如果有就把他压入自身的一个下标中
            $v['fenlei']=self::getCate($v['cid']);
            //将每次得到的数据遍历到空数组中
            $cate[]=$v;
        }
         return $cate;
    } 

	//显示个人中心主页面
    public function getIndex(Request $request){
        $cate=self::getCate(0);
        $h_uid=$request->input('h_uid');
        // dd($h_uid);
        $data=DB::table('home_user')->where('h_uid',$h_uid)->first();
        
        // 待发货数量
        $daifahuo = DB::table('order') -> where('h_uid',$h_uid) -> where('ostatus',0) -> count();
        // 待收货 已发货
        $yifahuo= DB::table('order') -> where('h_uid',$h_uid) -> where('ostatus',1) -> count();
        // 待评价
        $daipingjia = DB::table('order_detail') -> join('goods','order_detail.gid','=','goods.gid')-> join('order','order_detail.oid','=','order.oid')-> where('order.ostatus',2) -> where('order_detail.h_uid',$h_uid) -> where('order_detail.commented',0) -> count();


    	return view("/home/user/index",['cate'=>$cate,'h_uid'=>$h_uid,'data'=>$data,'daifahuo'=>$daifahuo,'yifahuo'=>$yifahuo,'daipingjia'=>$daipingjia]);
    }
    //完善信息等操作
    public function getInfo(Request $request){
        $h_uid=$request->input('h_uid');
        $data=DB::table('home_user')->where('h_uid',$h_uid)->get();
        // dd($data);
        $cate=self::getCate(0);
        return view('/home/user/info',['cate'=>$cate,'data'=>$data[0]]);
    }

    //修改用户提交的信息
    public function postUpdate(Request $request){
       

        $data=$request->except('_token');
        $res=DB::table('home_user')->where('h_uid',$data['h_uid'])->update($data);
        $info=DB::table('home_user')->where('h_uid',$data['h_uid'])->get();
        // dd($data);
        // session(['h_user'=>$info]);
        if($res>0){
            return redirect("/home/self/success?h_uid=".$data['h_uid'])->with('success','修改成功');
        }else{
            return redirect("/home/self/error?h_uid=".$data['h_uid'])->with('error','修改失败');
        }
    }
        //修改用户头像
        public function postUpic(Request $request){
             //检测是否有文件上传
        $h_uid=$request->input('h_uid');
        // dd($h_uid);
        if($request->hasFile('h_upic')){
            //获取文件后缀
            $hz=$request->file('h_upic')->getClientOriginalExtension();
            //文件名字
            $temp_name=md5(time()+rand(1111,9999));
            $name=$temp_name.'.'.$hz;
            $addr='/upload/user/'.$name;
            $res=DB::table('home_user')->where('h_uid','=',$h_uid)->update(['h_upic'=>$addr]);
            // dd($res);
            //将文件移动到新的位置
            
            if($res>0){
                $request->file('h_upic')->move('./upload/user',$name);
            }
            $data=DB::table('home_user')->where('h_uid','=',$h_uid)->first();
            // dd($data);
            session(['h_user'=>$data]);
             return redirect("/home/self/index?h_uid=$h_uid")->with('success','上传成功');
        }else{
            return back()->with('error','上传失败');
        }
    }

        //显示修改密码界面
       public function getEditpass(Request $request){
            $h_uid=$request->input('h_uid');
            $data=DB::table('home_user')->where('h_uid',$h_uid)->first();
            return view('/home/user/editpwd',['data'=>$data]);
        }

        //修改用户密码
        public function postUpdatepass(Request $request){
            $pwd=$request->only('h_upassword','h_uid');
             //验证密码是否正确
            if(empty($request->all())){
                return back()->with('perror','选项不能为空');

            }
            $data=DB::table('home_user')->where('h_uid',$pwd['h_uid'])->first();
            if(!(Hash::check($request->input('oldpassword'),$data['h_upassword']))) {
                return back()->with('oerror','原密码错误');
            }
            if($pwd['h_upassword']!=$request->input('repassword')){
                return back()->with('perror','两次密码不一致');
            }
            $pwd['h_upassword']=Hash::make($pwd['h_upassword']);
            $res=DB::table('home_user')->where('h_uid',$pwd['h_uid'])->update($pwd);

            if($res>0){
                return redirect("/home/self/success?h_uid=".$pwd['h_uid'])->with('success','修改成功');
            }else{
                return redirect("/home/self/error?h_uid=".$pwd['h_uid'])->with('error','修改成功');
            }
        }

        //显示正确信息提示页面
        public function getSuccess(Request $request){
            $h_uid=$request->input('h_uid');
            $cate=self::getCate(0);

            return view("/home/success",['cate'=>$cate,'h_uid'=>$h_uid]);
        }

        //显示错误信息提示页面
        public function getError(Request $request){
            $h_uid=$request->input('h_uid');
            $cate=self::getCate(0);
            // dd($h_uid);
            return view('/home/error',['cate'=>$cate,'h_uid'=>$h_uid]);
        }


        //一个静态的类 用来发送邮件
        static public function mailto($email,$h_uid,$token){
        Mail::send('/home/email/index', ['h_uid' => $h_uid,'token'=>$token], function ($m) use ($email) {
            //发送邮件 标题为subject内容
            $m->to($email)->subject('朵购网邮箱认证');
            });
        }


        //进行邮箱认证
        public function getEmail(Request $request){
            $h_uid=$request->input('h_uid');
            // dd($h_uid);
            $data=DB::table('home_user')->where('h_uid',$h_uid)->first();
            $reg='/^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/';
            
            if(!(preg_match($reg,$data['h_uemail']))){
                return redirect("/home/self/error?h_uid=$h_uid")->with('error','邮箱格式错误');
            }
            // if($data['h_uemail'])
            // dd($data);
            //发送邮件  调用静态类
            $res=self::mailto($data['h_uemail'],$h_uid,$data['token']);

            return redirect("/home/self/success?h_uid=$h_uid")->with('success','邮件发送成功');
        }


        //激活操作
        public function getJihuo(Request $request){
        $arr=array();
        //通过传过来的ID查询数据库中token  和参数中的toen做对比 如果匹配则进行修改激活操作
        $res=DB::table('home_user')->where('h_uid','=',$request->input('h_uid'))->select('token')->first();
        $h_uid=$request->input('h_uid');
        // dd($h_uid);
        //自定义数组用于修改状态
        $arr['h_ustatus']=2;
        // dd($res);
        if($res['token']==$request->input('token')){
            // echo  '11';
            $res2=DB::table('home_user')->where('h_uid','=',$h_uid)->update($arr);
            if($res2>0){
                return redirect("/home/self/success?h_uid=$h_uid")->with('success','账号认证成功');
            }else{
                return redirect("/home/self/error?h_uid=$h_uid")->with('error','账号认证失败');
            }
            
        }
        
    }

    //抽奖页面
    public function getChoujiang(){
        $data=DB::table('home_user')->select('h_utel','every')->get();
        foreach($data as $k=>$v){
            $str = $v['h_utel'];
            $ptn = '/(\d{3})(\d{4})(\d{4})/';
            $data[$k]['h_utel']=preg_replace($ptn,'\\1****\\3',$str);
        }
        
        return view('/home/choujiang/index',['data'=>$data]);
    }


    
    
}
